Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commit ID to version output #370

Merged
merged 1 commit into from
Jul 18, 2023

Conversation

mgoerens
Copy link
Contributor

  • add a "build" make target, which injects the git commit ID using ldflags
  • add the commit ID to the version output
  • add the possibility to get the version and commit information in a JSON format

Example output:

[mgoerens@mgoerens-thinkpadp1gen3 chart-verifier]$ ./chart-verifier version
chart-verifier v1.12.0 <commit: 1c1d51406d0241cbf9683b8dd731eb4bcb706dec>
[mgoerens@mgoerens-thinkpadp1gen3 chart-verifier]$ ./chart-verifier version --as-data
{"version":"1.12.0","commit":"1c1d51406d0241cbf9683b8dd731eb4bcb706dec"}[mgoerens@mgoerens-thinkpadp1gen3 chart-verifier]$ 

@komish
Copy link
Contributor

komish commented Jul 10, 2023

@mgoerens
Copy link
Contributor Author

This should be fixed now.

@mgoerens
Copy link
Contributor Author

Fixes #370

@komish komish linked an issue Jul 10, 2023 that may be closed by this pull request
Makefile Outdated
Comment on lines 51 to 54
.PHONY: build
build:
go build -ldflags "-X 'github.com/redhat-certification/chart-verifier/cmd.CommitIDLong=$(COMMIT_ID_LONG)'"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets use the existing bin target instead of having a separate build target. Also add this to bin_win for now.

I'll collapse those targets at some point in the future, but for now, we can duplicate the logic in both places.

cmd/version.go Outdated
func init() {
rootCmd.AddCommand(NewVersionCmd())
}

// CommitIDLong contains the commit ID the binary was build on. It is populated at build time by ldflags.
// If you're running from a local debugger it will show an empty commit ID.
var CommitIDLong string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's preseed this with a value of unknown

cmd/version.go Outdated
func NewVersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "print the chart-verifier version information",
Long: "git commit information for this particular binary build is included at build time and can be accessed by this command",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave the long description out

cmd/version.go Outdated
return err
}
fmt.Fprint(out, string(marshalledVersion))
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this else here. Instead, return nil in the if block after line 56.

E.g. (disregard pseudocode names, just demonstrating structure)

func runVersion(...) ... {
    if asData {
        printAsDataHopefullySuccessfully()
        return nil
    }
    
    printThingsNormally()
    return nil
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I was thinking about this and was balanced. Good that you have a strong opinion on this, that helps making a decision :)

cmd/version.go Show resolved Hide resolved
@komish
Copy link
Contributor

komish commented Jul 10, 2023

@mgoerens Left a couple of comments.

The CI complaint is an interesting way. Effectively what it's implying is that the chart-verifier version output is used in report generation, and since we've changed its output, we're breaking our fixtures. Normally, I'd say we can update our fixtures, however I think for this case, I think we want to make sure we're backwards compatible with the reports because changing that has some hefty implications.

Please let me know if you need any guidance with this. I'd also recommend you are rebased off of main at any given time before submitting your PR, just to rule out any differences between your branch and main when it comes to CI. It can be a little finicky with differences between a PR and main.

@komish
Copy link
Contributor

komish commented Jul 10, 2023

Quick note, the problem is two-fold.

  1. Your CLI version is no longer using the library at pkg/chartverifier/version.GetVersion. We need to make sure we're using this because this is shared across both the CLI and the library use cases.

  2. Our tests are asserting that the version in generated reports matches the version string in the binary that produces them. We're expecting this to look a specific way ("vX.Y.Z") because this is then normalized and used for comparison against the report.

With this in mind, you can take this one of two ways, I think:

a) The easiest thing to do is just to add the commit information as a flag value. In other words, leave chart-verifier version untouched. Add a --commit flag that spits out the commit. Leave --as-data as you have it. This is backwards compatible but also adds the new features, even if isn't the best UX. You might also introduce pkg/chartverifier/version.GetCommit to facilitate this.

b) Update the e2e tests[1] to comply with expectations. Maybe it can use the --as-data value as a dictionary and be more reliable? This is trickier because these can be finicky.

I'm perfectly fine with both options. Your choice!

[1]https://github.com/redhat-certification/chart-verifier/blob/main/tests/tests/functional/chart_test.py#L57-L86

@mgoerens
Copy link
Contributor Author

The code does still make use of pkg/chartverifier/version.GetVersion

include apiversion "github.com/redhat-certification/chart-verifier/pkg/chartverifier/version"

[...]

var Version = VersionContext{
	Version: apiversion.GetVersion(),
	Commit:  CommitIDLong,
}

I ran make test prior to submitting the PR and was happy to get a green light. I didn't realize their are also a set of python tests ?

I'll look at option a) and b) in details tomorrow.

@komish
Copy link
Contributor

komish commented Jul 10, 2023

The code does still make use of pkg/chartverifier/version.GetVersion

Aha, missed this. Thanks for pointing it out!

@mgoerens mgoerens force-pushed the add_commit_id_to_version branch 10 times, most recently from 4dcd90a to 4d8a725 Compare July 17, 2023 15:24
@mgoerens
Copy link
Contributor Author

All right. Pipeline passes. Though I still don't manage to reproduce it locally, the error seemed to be coming from the lack of newline in the output of version --as-data. I can't understand why, but only some docker tests were failing due to that, and not always the same were failing depending on which one were run. For instance in this pipeline where I commented out the first docker test + all podman and tarball tests, only the 5th and 6th test pass (out of seven). If I run all tests only the first docker test passes, and following docker tests fail (all podman and tarball tests pass regardless). Go figure ... Besides, I cannot reproduce this behavior on a fresh debian instance.

So I added a newline and now all tests pass.

Full list of changes (from commit message):

- use ldflags to injects the git commit ID at build time
- add the commit ID to the version output
- add the possibility to get the version and commit information in a
  JSON format
- adapt the tests to make use of the JSON output
- adapt Dockerfile to make use of the make bin target
- fix path to chart used to test the built docker image in buildandtest
  script (moved as part of !271) and make use of the JSON output

Reg. the last point: the "build-and-test" script is, as the name suggests, comprised of two parts: first it builds the container image, then it tests the image using verify on a known chart. The script is called with the --build-only flag in the pipeline i.e. the image is not tested using this script.

There are multiple issues with the "test" part which makes this script not usable as-is (prior to this PR):

  • the chart used for testing has been moved to another location (easy fix)
  • the verify command uses a -l flag which isn't supported anymore (easy fix)
  • the verify command requires access to a Kubernetes cluster. Currently there is no way to provide Kubernetes credentials to the script (fixable, with some effort)

But before putting further effort into fixing the "test" part of this script, it appears to me like we don't need this script to be able to test a docker image anymore. The pipeline takes care of that in a next step (actually running some extensive tests). AFAICT this script is never called without the --build-only flag in our testing / release process (and anyway it would not have worked, even prior to this PR).

My suggestion is to not fix this part and to consider dropping the "test" part of the buildandtest script altogether in another PR.

@mgoerens
Copy link
Contributor Author

Opened #378 for the buildandtest issue

- use ldflags to injects the git commit ID at build time
- add the commit ID to the version output
- add the possibility to get the version and commit information in a
  JSON format
- adapt the tests to make use of the JSON output
- adapt Dockerfile to make use of the make bin target
- fix path to chart used to test the built docker image in buildandtest
  script (moved as part of !271) and make use of the JSON output

close redhat-certification#362

Signed-off-by: mgoerens <[email protected]>
Copy link
Contributor

@komish komish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

Thanks @mgoerens!

@komish komish merged commit 40ba87f into redhat-certification:main Jul 18, 2023
@mgoerens mgoerens deleted the add_commit_id_to_version branch July 31, 2023 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add commit information to version output of chart-verifier
2 participants